package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;

/**
 * Created by Administrator on 2018/10/23 0023.
 */

public class ChangeUserNameActivity extends AppCompatActivity implements View.OnClickListener {

    EditText nameEdit;
    RegisterResult loginResult;
    String oldName;
    TextView saveBtn;
    ImageView backView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                , WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_change_username);
        initView();
        loginResult = Preferences.getInstance().getLoginResult(this);
        oldName = loginResult.getNicename();
    }

    private void initView() {
        nameEdit = findViewById(R.id.change_username_edit);
        saveBtn = findViewById(R.id.change_username_save);
        backView=findViewById(R.id.change_username_back);
        saveBtn.setOnClickListener(this);
        backView.setOnClickListener(this);
    }

    private void changeName() {
        HttpHelper.getUserCenter().memberChangeInfo(QMap.build()
                .put("user_id", Integer.parseInt(loginResult.getUid()))
                .put("nicename", nameEdit.getText().toString())
                .put("sex", "1")
                .put("logincode", loginResult.getLogincode()).create()).enqueue(new ResultCallBack<Result<Object>>(this) {
            @Override
            public void onSuccessCallback(int code, Result<Object> body) {
                if (body != null) {
                    ToastUtil.showToast(ChangeUserNameActivity.this, body.getMsg());
                    loginResult.setNicename(nameEdit.getText().toString());
                    finish();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == saveBtn) {
            if (TextUtils.isEmpty(nameEdit.getText())) {
                ToastUtil.showToast(this, "昵称不能为空");
                return;
            }
            if (nameEdit.getText().toString().equals(oldName)) {
                ToastUtil.showToast(this, "昵称不能与原来一致");
                return;
            }
            changeName();
        }else if(v==backView){
            finish();
        }
    }
}
